<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      FeatherCMS
 * @subpackage   Libs
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://julien.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class Typographer
{
	static function transforms($text)
	{
		// entities
		
		$from = array(
			// non-HTML curly quotes
			'/(?!<.*?)"([^"<>]+?)"(?![^<>]*?>)/',
			"/``(.+?)''/",
		);
		$text = preg_replace($from, '“\1”', $text);
		
		$from = array(
			// arrows (<->, ->, <-, <=>, =>, <=>)
			'&lt;-&gt;', '-&gt;', '&lt;-',
			'&lt;=&gt;', '=&gt;', '&lt;=',
			// french quotes (<< >>)
			'&lt;&lt;', '&gt;&gt;',
			// dashes (em, en)
			'---', '--',
			//
			'...',
			'(c)', '(tm)', '(r)'
		);
		$to = array(
			// arrows
			'&harr;', '&rarr;', '&larr;',
			'&hArr;', '&rArr;', '&lArr;',
			// french quotes
			'«', '»',
			// dashes
			'&mdash;', '&ndash;',
			//
			'&#8230;',
			'&copy;', '&trade;', '&reg;'
		);
		$text = str_replace($from, $to, $text);
		
		
		// spaces
		$from = array(
			'/(\s+)(:|;|!|\?)/',
			'/(¿|¡)(\s+)/',
#			'/(«|&laquo;)\s+(.+?)\s+(»|&raquo;)/',
			'/«\s*(.+?)\s*»/',
			'/(\d)\s+(\d)/',
		);
		$to = array(
			'&nbsp;\2',
			'\1&nbsp;',
#			'&laquo;&nbsp;\2&nbsp;&raquo;',
			'&laquo;&nbsp;\1&nbsp;&raquo;',
			'\1&nbsp;\2',
		);
		$text = preg_replace($from, $to, $text);
		
		return $text;
	}
	
}
?>